<?php

namespace catchAdmin\stock\model;

use catchAdmin\stock\model\search\LockingSearch;
use catcher\base\CatchModel;
use think\facade\Db;

/**
 *
 * @property int $id
 * @property string $warehouse_out_id
 * @property string $warehouse_out_son_id
 * @property string $pick_id
 * @property string $tray_id
 * @property string $box_id
 * @property string $slice_id
 * @property int $number
 * @property string $status
 * @property int $auth_id
 * @property int $creator_id
 * @property int $updated_at
 * @property int $created_at
 * @property int $deleted_at
 * @property int $tenant_id
 */
class Locking extends CatchModel
{
    use LockingSearch;
    
    public $field = [
        // ID
        'id',
        // 出库主单编号
        'warehouse_out_id',
        // 出库明细编号
        'warehouse_out_son_id',
        // 拣货编号
        'pick_id',
        // 托盘码
        'tray_id',
        // 箱号
        'box_id',
        // 片号
        'slice_id',
        // 拣货数量
        'number',
        // 出库状态:0=未出库,1=出库,
        'status',
        // 出库人
        'auth_id',
        // 拣货人
        'creator_id',
        // 更新时间（出库时间）
        'updated_at',
        // 创建时间
        'created_at',
        // 删除时间
        'deleted_at',
        // 商户
        'tenant_id',
    ];
    
    public $name = 'db_pick';
    /**
     * 列表
     *
     * @time 2020年01月09日
     * @param $params
     * @throws \think\db\exception\DbException
     * @return \think\Paginator
     */
    public function getList($tenant_id = 0,$key='',$creator_id=0)
    {
        $where=[];
        if($key){
            $where[]=['b.warehouse_out_id|b.tray_id|d.name','like',"%$key%"];
        }
//        if($tenant_id){
//            $where[]=['b.tenant_id','=',$tenant_id];
//        }
        if($creator_id){
            $where[]=['b.creator_id','=',$creator_id];
        }
        $where[]=['b.deleted_at','=',0];
        $where[]=['b.status','=',0];
        return $this->catchSearch()
            ->alias('b')
            ->leftJoin('db_stock c','b.tray_id = c.tray_id')
            ->leftJoin('db_warehouse_config d','c.config_id = d.config_id')
            ->leftJoin('users e','b.creator_id = e.id')
            ->field('b.*,d.name as config_name,e.username')
            ->where($where)
            ->paginate();
    }


    public function getList2($tenant_id = 0,$key='',$creator_id=0)
    {
        $where=[];
        if($key){
            $where[]=['b.warehouse_out_id|b.tray_id|d.name','like',"%$key%"];
        }
//        if($tenant_id){
//            $where[]=['b.tenant_id','=',$tenant_id];
//        }
        if($creator_id){
            $where[]=['b.creator_id','=',$creator_id];
        }
        $where[]=['b.deleted_at','=',0];
        $where[]=['b.status','=',0];
        return $this->catchSearch()
            ->alias('b')
            ->leftJoin('users e','b.creator_id = e.id')
            ->leftJoin('db_warehouse_out_son wos','b.warehouse_out_son_id = wos.warehouse_out_son_id')
            ->field('b.*,e.username,wos.goods_name')
            ->where($where)
            ->order('id desc')
            ->paginate()
            ->each(function (&$item){
               $config_id = Db::table('db_stock')->where('tray_id',$item['tray_id'])->value('config_id');
               $item['config_name'] = Db::table('db_warehouse_config')->where('config_id',$config_id)->value('name');
            });
    }
}